package com.fenghuajueli.libbasecoreui;

import android.app.Application;
import android.content.pm.PackageInfo;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.MetaDataUtils;
import com.fenghuajueli.lib_data.entity.db.DaoMaster;
import com.fenghuajueli.libbasecoreui.constants.ConfigConstants;
import com.fenghuajueli.libbasecoreui.constants.SwitchConfig;
import com.fenghuajueli.lib_statistics.UmEventUtils;
import com.fenghuajueli.lib_data.database.DaoUtils;
import com.fenghuajueli.lib_net.BuildHeadersListener;
import com.fenghuajueli.lib_net.common.RetrofitUtils;
import com.fenghuajueli.libbasecoreui.utils.MmkvUtils;
import com.tencent.mmkv.MMKV;
//import com.umeng.commonsdk.UMConfigure;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseApplication extends Application {

    private static BaseApplication application;
    private static DaoMaster daoMaster;

    public static BaseApplication getApplication() {
        return application;
    }

    public static final String FILE_DIR = "TimeManageOffice";

    /**
     * 是否是debug模式
     *
     * @return
     */
    public abstract boolean appIsDebug();

    /**
     * 获取渠道名称
     *
     * @return
     */
    public abstract String getChannel();

    /**
     * 返回友盟的key，隐私合规整改后用于预先初始化
     *
     * @return
     */
    public String getUmAppKey() {
        return MetaDataUtils.getMetaDataInApp("UMENG_APPKEY");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        initNormalSdk();
        if (MmkvUtils.get(ConfigConstants.MMKVKEY.privacyDialogIsShow.name(), false)) {
            initThirdSdk();
        }
    }

    public void initThirdSdk() {
//        UMConfigure.setLogEnabled(appIsDebug());
//        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE, "");
    }

    public void initNormalSdk() {
        if (appIsDebug()) {           // 这两行必须写在init之前，否则这些配置在init过程中将无效
            ARouter.openLog();     // 打印日志
            ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        }
//        UMConfigure.preInit(application, getUmAppKey(), getChannel());
        MMKV.initialize(this);
        SwitchConfig.currentChannel = getChannel();
        ARouter.init(this);
        DaoUtils.init(this);
        RetrofitUtils.init(this, new BuildHeadersListener() {
            @Override
            public Map<String, String> buildHeaders() {
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("User-Agent", getPackageName() + "/" + getVersionName());
                return hashMap;
            }
        });
        UmEventUtils.init(this);
    }


    private String getVersionName() {
        try {
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            return "unKnow Version";
        }
    }

    private static DaoMaster getDaoMaster(String dbName) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(application, dbName, null);
        daoMaster = new DaoMaster(helper.getWritableDatabase());
        return daoMaster;
    }

    public static DaoMaster getTimeBlockDaoMaster() {
        return getDaoMaster("timeblock.db");
    }

    public static DaoMaster getMemoDaoMaster() {
        return getDaoMaster("memo.db");
    }

    public static DaoMaster getMattersDaoMaster() {
        return getDaoMaster("matters.db");
    }

    public static DaoMaster getTargetDaoMaster() {
        return getDaoMaster("target.db");
    }

    public static DaoMaster getNoteDaoMaster() {
        return getDaoMaster("note.db");
    }

}
